<template>
  <div class="admin-container">
    <!-- 顶部导航栏 -->
    <div class="header">
      <div class="header-left">
        <div class="logo">
          <el-icon class="logo-icon">
            <monitor />
          </el-icon>
          <span>管理系统</span>
        </div>
      </div>
      <div class="header-right">
        <el-tooltip content="消息通知" placement="bottom">
          <el-badge :value="12" class="notification">
            <el-icon>
              <bell />
            </el-icon>
          </el-badge>
        </el-tooltip>
        <el-dropdown>
          <span class="user-info">
            <el-avatar :size="32"
              :src="userInfo?.headPicture || 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'" />
            <span class="username">{{ userInfo?.realname || '未登录' }}</span>
            <el-icon><arrow-down /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="handleProfile">
                <el-icon>
                  <user />
                </el-icon> 个人信息
              </el-dropdown-item>
              <el-dropdown-item @click="handleChangePassword">
                <el-icon>
                  <key />
                </el-icon> 修改密码
              </el-dropdown-item>
              <el-dropdown-item divided @click="handleLogout">
                <el-icon><switch-button /></el-icon> 退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <div class="main-content">
      <!-- 侧边栏 -->
      <div class="sidebar">
        <el-menu :default-active="activeIndex" class="el-menu-vertical" background-color="#304156" text-color="#bfcbd9"
          active-text-color="#409EFF" @select="handleSelect">
          <el-menu-item index="/home">
            <el-icon><home-filled /></el-icon>
            <span>首页</span>
          </el-menu-item>

          <el-sub-menu index="/system">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>系统管理</span>
            </template>
            <el-menu-item index="/user/msg">
              <el-icon>
                <user />
              </el-icon>
              <span>用户管理</span>
            </el-menu-item>
            <el-menu-item index="/userroleGroup">
              <el-icon><user-filled /></el-icon>
              <span>角色组管理</span>
            </el-menu-item>
            <el-menu-item index="/permission/bind-permission">
              <el-icon><Menu /></el-icon>
              <span>权限管理</span>
            </el-menu-item>
          </el-sub-menu>

          <el-menu-item index="/orders">
            <el-icon>
              <tickets />
            </el-icon>
            <span>订单管理</span>
          </el-menu-item>
          <el-menu-item index="/data">
            <el-icon><data-line /></el-icon>
            <span>数据统计</span>
          </el-menu-item>
          <el-menu-item index="/rayfile">
            <el-icon><folder /></el-icon>
            <span>共享网盘</span>
          </el-menu-item>

          <el-sub-menu index="/workflow">
            <template #title>
              <el-icon><Operation /></el-icon>
              <span>流程管理</span>
            </template>
            <el-menu-item index="/workflow/process-definition">
              <el-icon><List /></el-icon>
              <span>流程定义</span>
            </el-menu-item>
            <el-menu-item index="/workflow/form-config">
              <el-icon><Document /></el-icon>
              <span>表单配置</span>
            </el-menu-item>
          </el-sub-menu>

          <el-sub-menu index="/task">
            <template #title>
              <el-icon><Operation /></el-icon>
              <span>任务管理</span>
            </template>
            <el-menu-item index="/task/originating-task">
              <el-icon><List /></el-icon>
              <span>发起任务</span>
            </el-menu-item>
            
          </el-sub-menu>
          
        </el-menu>
      </div>

      <!-- 主要内容区域 -->
      <div class="content" :class="{ 'process-content': isProcessPage }">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script>
import { 
  ArrowDown,
  HomeFilled,
  User,
  Tickets,
  DataLine,
  Money,
  ChatDotRound,
  Bell,
  Monitor,
  Plus,
  Key,
  SwitchButton,
  Folder,
  Operation,
  Document,
  Upload,
  List,
  Timer
} from '@element-plus/icons-vue'
import { mapState } from 'vuex'

export default {
  name: 'Home',
  components: {
    ArrowDown,
    HomeFilled,
    User,
    Tickets,
    DataLine,
    Money,
    ChatDotRound,
    Bell,
    Monitor,
    Plus,
    Key,
    SwitchButton,
    Folder,
    Operation,
    Document,
    Upload,
    List,
    Timer
  },
  data() {
    return {
      activeIndex: '/home'
    }
  },
  computed: {
    ...mapState({
      userInfo: state => state.userInfo
    }),
    currentRoute() {
      return this.$route.path
    },
    isProcessPage() {
      return this.$route.path.startsWith('/process')
    }
  },
  watch: {
    currentRoute: {
      immediate: true,
      handler(newPath) {
        this.activeIndex = newPath
      }
    }
  },
  methods: {
    handleSelect(key) {
      this.$router.push(key)
    },
    handleProfile() {
      this.$router.push('/user/profile')
    },
    handleChangePassword() {
      this.$router.push('/change-password')
    },
    handleLogout() {
      this.$msgbox.confirm(
        '退出登录？',
        '提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(() => {
        // 清除用户信息
        localStorage.removeItem('token')
        localStorage.removeItem('userInfo')
        // 清除 Vuex store 中的 userInfo
        this.$store.commit('SET_USER_INFO', null)

        // 跳转到登录页
        this.$router.push('/login')

        // 提示用户
        this.$message({
          message: '退出登录成功',
          type: 'success'
        })
      })
    }
  },
}
</script>

<style scoped>
.admin-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
}

.header {
  height: 60px;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: bold;
  color: #409EFF;
  cursor: pointer;
  transition: all 0.3s;
}

.logo:hover {
  transform: scale(1.05);
}

.logo-icon {
  margin-right: 10px;
  font-size: 24px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.notification {
  cursor: pointer;
  font-size: 20px;
  color: #606266;
  transition: all 0.3s;
}

.notification:hover {
  color: #409EFF;
}

.user-info {
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px;
  border-radius: 4px;
  transition: all 0.3s;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.username {
  font-size: 14px;
  color: #606266;
}

.main-content {
  position: fixed;
  top: 60px;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
}

.sidebar {
  width: 200px;
  background-color: #304156;
  position: fixed;
  top: 60px;
  bottom: 0;
  left: 0;
  z-index: 99;
}

.content {
  position: fixed;
  top: 60px;
  left: 200px;
  right: 0;
  bottom: 0;
  background-color: #f5f7fa;
  overflow-y: auto;
  padding: 16px;
}

.process-content {
  padding: 16px;
}

.content > * {
  height: 100%;
}

.welcome-card {
  background-color: #fff;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
}

.welcome-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.welcome-header h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.statistics {
  margin-top: 20px;
}

.stat-card {
  transition: all 0.3s;
  border: none;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.stat-item {
  display: flex;
  align-items: center;
}

.stat-icon {
  font-size: 48px;
  margin-right: 15px;
  padding: 12px;
  border-radius: 8px;
}

.user-icon {
  color: #409EFF;
  background-color: rgba(64,158,255,.1);
}

.order-icon {
  color: #67C23A;
  background-color: rgba(103,194,58,.1);
}

.money-icon {
  color: #E6A23C;
  background-color: rgba(230,162,60,.1);
}

.message-icon {
  color: #F56C6C;
  background-color: rgba(245,108,108,.1);
}

.stat-info {
  display: flex;
  flex-direction: column;
}

.stat-value {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  line-height: 1.2;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-top: 8px;
}

.el-menu-vertical {
  border-right: none;
  height: 100%;
}

.el-menu-item {
  height: 50px;
  line-height: 50px;
  margin: 4px 0;
}

.el-menu-item:hover {
  background-color: #263445 !important;
}

.el-menu-item.is-active {
  background-color: #263445 !important;
}

.el-dropdown-menu__item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.el-dropdown-menu__item .el-icon {
  font-size: 16px;
}
</style>